<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { reqSkuListAPI, reqSkuOnSaleAPI, reqSkuCancelSaleAPI, reqSkuInfoAPI, reqDeleteSkuAPI } from '@/apis/product/sku'
import type { SkuData } from '@/apis/product/sku/type'
import { ElMessage } from 'element-plus'
//分页器当前页码
let pageNo = ref(1)
//分页器每页数量
let pageSize = ref(10)
//储存sku接口数据
let skuList = ref<SkuData[]>([])
let total = ref(0)
//控制查看弹窗显示与隐藏
let isShow = ref(false)
//获取sku列表
const getSkuList = async (pager = 1) => {
    pageNo.value = pager
    let result = await reqSkuListAPI(pageNo.value, pageSize.value)
    if (result.code == 200) {
        skuList.value = result.data.records
        total.value = result.data.total
    }
}
onMounted(() => {
    getSkuList()
})
// 分页器每页数量发生改变触发,回到第一页
const handler = () => {
    getSkuList()
}
// 点击table上架和下架的操作
const updateSale = async (row: SkuData) => {
    if (row.isSale == 0) {
        //点击下架按钮后，发送下架接口
        const result: any = await reqSkuOnSaleAPI((row.id as number))
        if (result.code == 200) {
            ElMessage.success('下架成功')
            getSkuList(pageNo.value)
        }
    } else {
        //点击上架按钮后，发送上架接口
        const result: any = await reqSkuCancelSaleAPI((row.id as number))
        if (result.code == 200) {
            ElMessage.success('上架成功')
            getSkuList(pageNo.value)
        }
    }
}
//点击页面编辑按钮点击回调函数
const editSku = (row: SkuData) => {
    ElMessage.success('暂无此功能')
}
//储存商品信息接口数据
let skuInfo = ref<any>({})
//点击页面查看按钮
const toLookSku = async (row: SkuData) => {
    isShow.value = true
    const result = await reqSkuInfoAPI((row.id as number))
    skuInfo.value = result.data
    console.log(skuInfo.value)
}
//点击页面删除按钮点击回调函数
const deleteSku = async (row: SkuData) => {
    //点击删除按钮后，发送删除接口
    const result: any = await reqDeleteSkuAPI(row.id as number)
    if (result.code === 200) {
        ElMessage.success('删除成功')
        getSkuList(pageNo.value)
    }

}
</script>
<template>
    <el-card>
        <el-table border :data="skuList">
            <el-table-column label="序号" type="index" width="80" align="center"> </el-table-column>
            <el-table-column label="名称" show-overflow-tooltip width="150" prop="skuName"> </el-table-column>
            <el-table-column label="描述" show-overflow-tooltip width="150" prop="skuDesc"> </el-table-column>
            <el-table-column label="图片" width="150">
                <template #="{ row }">
                    <!-- preview-src-list 开启图片预览功能 -->
                    <!-- preview-teleported="true" 允许图片预览功能显示在当前页面上 -->
                    <el-image :src="row.skuDefaultImg" :preview-src-list="[row.skuDefaultImg]"
                        :preview-teleported="true" alt="图片" style="width: 100px; height: 100;"></el-image>
                </template>
            </el-table-column>
            <el-table-column label="重量" prop="weight"> </el-table-column>
            <el-table-column label="价格" prop="price"> </el-table-column>
            <el-table-column label="操作" fixed="right">
                <template #="{ row }">
                    <el-button type="primary" @click="updateSale(row)">{{ row.isSale ? '上架' : '下架' }}</el-button>
                    <el-button type="danger" @click="deleteSku(row)">删除</el-button>
                    <el-button type="warning" @click="editSku(row)">编辑</el-button>
                    <el-button type="info" @click="toLookSku(row)">查看</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination class="spu-pagination" :pager-count="5" background v-model:current-page="pageNo"
            v-model:page-size="pageSize" :page-sizes="[3, 5, 7, 10]" layout="prev, pager, next, jumper, ->,sizes, total"
            :total="total" @current-change="getSkuList" @size-change="handler">
        </el-pagination>
        <!-- 页面查看sku的对话框 -->
        <el-drawer title="查看sku详情" v-model="isShow" size="50%" direction="rtl">
            <template #default>
                <el-form>
                    <el-form-item label="sku名称">{{ skuInfo.skuName }}</el-form-item>
                    <el-form-item label="sku描述">{{ skuInfo.skuDesc }}</el-form-item>
                    <el-form-item label="价格">{{ skuInfo.price }}</el-form-item>
                    <el-form-item label="重量">{{ skuInfo.weight }}</el-form-item>
                    <el-form-item label="平台属性">
                        <el-tag v-for="(item, index) in skuInfo.skuAttrValueList" :key="index">{{ item.attrName }}:{{
                            item.valueName
                            }}</el-tag>
                    </el-form-item>
                    <el-form-item label="销售属性">
                        <el-tag v-for="(item, index) in skuInfo.skuSaleAttrValueList" :key="index">{{ item.saleAttrName
                        }}:{{ item.saleAttrValueName }}</el-tag>
                    </el-form-item>
                    <el-form-item label="图片">
                        <!-- 图片轮播 -->
                        <el-carousel :interval="4000" type="card" height="200px">
                            <el-carousel-item v-for="item in skuInfo" :key="item.id">
                                <img :src="item.skuDefaultImg" alt="">
                            </el-carousel-item>
                        </el-carousel>
                    </el-form-item>
                </el-form>
            </template>
        </el-drawer>
    </el-card>
</template>
<style scoped>
.spu-pagination {
    margin-top: 20px;
}

.el-carousel__item h3 {
    color: #475669;
    opacity: 0.75;
    line-height: 200px;
    margin: 0;
    text-align: center;
}

.el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
    background-color: #d3dce6;
}
</style>